//
//  AKRequest_Factory.m
//  CocoaBean
//
//  Created by sleeper on 6/3/10.
//  Copyright 2010 __MyCompany__ All rights reserved.
//

#import "AKRequest_Private.h"
#import "AKRequest_Factory.h"

NSString * const kAKRequestTypeUserInfo = @"userInfo";
NSString * const kAKRequestTypeMiniblogEntries = @"miniblogEntries";
NSString * const kAKRequestTypeMiniblogComments = @"miniblogComments";

NSString * const kAKRequestTypeMediaPrefix		= @"media_";
NSString * const kAKRequestTypeISBNSuffix		= @"ISBN";
NSString * const kAKRequestTypeBook				= @"media_book";
NSString * const kAKRequestTypeMovie			= @"media_movie";
NSString * const kAKRequestTypeMusic			= @"media_music";
NSString * const kAKRequestTypeBookISBN			= @"media_bookISBN";
//NSString * const kAKRequestTypeMovieISBN		= @"media_movieISBN";
//NSString * const kAKRequestTypeMusicISBN		= @"media_musicISBN";

NSString * const kAKRequestTypeSearchPrefix		= @"search_";
NSString * const kAKRequestTypeSearchBook		= @"search_book";
NSString * const kAKRequestTypeSearchMovie		= @"search_movie";
NSString * const kAKRequestTypeSearchMusic		= @"search_music";

NSString * const kAKRequestTypePostPrefix		= @"post_";
NSString * const kAKRequestTypePostSaying		= @"post_saying";
NSString * const kAKRequestTypePostComment		= @"post_comment";

NSString * const kAKPostSayingRequestContentFormatString = @"<?xml version='1.0' encoding='UTF-8'?><entry xmlns:ns0=\"http://www.w3.org/2005/Atom\" xmlns:db=\"http://www.douban.com/xmlns/\"><content>%@</content></entry>";
NSString * const kAKPostCommentRequestContentFormatString = @"<?xml version='1.0' encoding='UTF-8'?><entry><content>%@</content></entry>";

@implementation AKRequest (Factory)

+ (AKRequest *)requestWithType:(NSString *)type
{
	return [self requestWithType:type extraInfo1:nil extraInfo2:nil];
}

+ (AKRequest *)requestWithType:(NSString *)type extraInfo:(NSString *)extraInfo
{
	return [self requestWithType:type extraInfo1:extraInfo extraInfo2:nil];
}

+ (AKRequest *)requestWithType:(NSString *)type extraInfo1:(NSString *)extraInfo extraInfo2:(NSString *)extraInfo2
{
	if ([type length] == 0)
		return nil;
	
	AKRequest *request = nil;
	
	if ([type isEqualToString:kAKRequestTypeUserInfo]) {
		request = [[AKRequest new] autorelease];
		request.url = [NSURL URLWithString:@"http://api.douban.com/people/%40me"];
	}
	else if ([type isEqualToString:kAKRequestTypeMiniblogEntries]) {
		if ([extraInfo length] > 0) {
			request = [[AKRequest new] autorelease];
			request.url = [NSURL URLWithString:[NSString stringWithFormat:@"http://api.douban.com/people/%@/miniblog/contacts", extraInfo]];
		}
	}
	else if ([type isEqualToString:kAKRequestTypeMiniblogComments]) {
		if ([extraInfo length] > 0) {
			request = [[AKRequest new] autorelease];
			request.url = [NSURL URLWithString:[NSString stringWithFormat:@"http://api.douban.com/miniblog/%@/comments", extraInfo]];
		}
	}
	else if ([type hasPrefix:kAKRequestTypeMediaPrefix]) {
		// Book, music and movie
		if ([extraInfo length] > 0) {
			NSMutableString * typeString = [NSMutableString stringWithString:type];
			[typeString deleteCharactersInRange:NSMakeRange(0, [kAKRequestTypeMediaPrefix length])];
			BOOL hasISBN = [typeString hasSuffix:kAKRequestTypeISBNSuffix];
			if (hasISBN) {
				NSUInteger suffixLength = [kAKRequestTypeISBNSuffix length];
				[typeString deleteCharactersInRange:NSMakeRange([typeString length] - suffixLength, suffixLength)];
			}
			NSString * URLString = (hasISBN ? 
									[NSString stringWithFormat:@"http://api.douban.com/%@/subject/isbn/%@", typeString, extraInfo] : 
									[NSString stringWithFormat:@"http://api.douban.com/%@/subject/%@", typeString, extraInfo]);
			request = [[AKRequest new] autorelease];
			request.url = [NSURL URLWithString:URLString];
		}
	}
	else if ([type hasPrefix:kAKRequestTypeSearchPrefix]) {
		// Search book, movie and music
		if ([extraInfo length] > 0) {
			NSString *typeString = [type stringByReplacingOccurrencesOfString:kAKRequestTypeSearchPrefix withString:@""];
			NSString *URLString = [NSString stringWithFormat:@"http://api.douban.com/%@/subjects", typeString];
			request = [[AKRequest new] autorelease];
			request.url = [NSURL URLWithString:URLString];
			[request.parameters setObject:extraInfo forKey:@"tag"];
		}
	}
	else if ([type isEqualToString:kAKRequestTypePostSaying]) {
		if ([extraInfo length] > 0) {
			request = [[AKRequest new] autorelease];
			request.httpMethod = @"POST";
			request.url = [NSURL URLWithString:@"http://api.douban.com/miniblog/saying"];
			request.postContent = [NSString stringWithFormat:kAKPostSayingRequestContentFormatString, extraInfo];
		}
	}
	else if ([type isEqualToString:kAKRequestTypePostComment]) {
		if ([extraInfo length] > 0 && [extraInfo2 length] > 0) {
			request = [[AKRequest new] autorelease];
			request.httpMethod = @"POST";
			request.url = [NSURL URLWithString:[NSString stringWithFormat:@"http://api.douban.com/miniblog/%@/comments", extraInfo]];
			request.postContent = [NSString stringWithFormat:kAKPostCommentRequestContentFormatString, extraInfo2];
		}
	}
	
	if (request)
		request.type = type;
	
	return request;
}

@end
